/*
 * Copyright 2018-10-14 21:39:18 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.utils.DictionaryUtils;
import com.lap.scp.erp.ao.CustomerAO;
import com.lap.scp.erp.bean.PayCount;
import com.lap.scp.erp.domain.CustomerDO;
import com.lap.scp.erp.query.CustomerQueryVO;

/**
 * <pre>
 *  客户信息，提供给公司工程、项目控制对象，提供 客户信息，提供给公司工程、项目 新增,编辑,查询等操作。
 *  控制层统一命名规则：
 *  进入新增 	add,
 *  增加保存 	save,
 *  进入修改 	edit,
 *  修改保存 	update,
 *  查询分页 	queryPage,
 *  根据ID查询 	get,
 *  删除 		deleteById
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-14 21:39:18
 * @since 2.0.0
 */
@Controller
@RequestMapping("customer")
public class CustomerController {
	/**
	 * 客户信息，提供给公司工程、项目应用流程对象
	 */
	@Resource
	private CustomerAO customerAO;

	/**
	 * <pre>
	 * 进入新增客户信息，提供给公司工程、项目
	 * </pre>
	 * 
	 * @return
	 */
	@GetMapping(value = "add")
	public ModelAndView add() {
		ModelAndView andView = new ModelAndView("erp/customerEdit");
		andView.addObject("customerDO", new CustomerDO());
		andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		return andView;
	}

	/**
	 * <pre>
	 * 新增保存客户信息，提供给公司工程、项目
	 * </pre>
	 * 
	 * @param customerDO
	 * @param error
	 * @return
	 */
	@PostMapping(value = "add")
	public ModelAndView add(CustomerDO customerDO, BindingResult error) {
		ModelAndView andView = new ModelAndView();

		Resp<CustomerDO> resp = customerAO.insertCustomer(customerDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/customer/list");
		} else {
			andView.setViewName("erp/customerEdit");
			andView.addObject("customerDO", customerDO);
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据ID删除客户信息，提供给公司工程、项目
	 * </pre>
	 * 
	 * @param customerId
	 * @param map
	 * @param attributes
	 * @return
	 */
	@GetMapping(value = "delete/{customerId}")
	public ModelAndView deleteById(@PathVariable Integer customerId, @RequestParam Map<String, Object> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = customerAO.deleteById(customerId);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/customer/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据ID查询客户信息，提供给公司工程、项目
	 * </pre>
	 * 
	 * @param customerId
	 * @return
	 */
	@GetMapping(value = "get/{customerId}")
	public ModelAndView get(@PathVariable Integer customerId) {
		ModelAndView andView = new ModelAndView();
		Resp<CustomerDO> resp = customerAO.queryById(customerId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/customerDetail");
			andView.addObject("customerDO", resp.getData());
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据ID统计客户所有的合同金额信息
	 * </pre>
	 * 
	 * @param customerId
	 * @return
	 */
	@GetMapping(value = "getStatis/{customerId}")
	public ModelAndView getStatis(@PathVariable Integer customerId) {
		ModelAndView andView = new ModelAndView();
		Resp<PayCount> resp = customerAO.queryStatis(customerId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/customerPay");
			andView.addObject("payCount", resp.getData());
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据条件、分页信息查询客户信息，提供给公司工程、项目
	 * </pre>
	 * 
	 * @param page
	 * @param queryVO
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryPage(Page page, CustomerQueryVO queryVO) {
		ModelAndView andView = new ModelAndView();

		PageResult<CustomerDO> pr = customerAO.queryPage(page, queryVO);
		if (pr.isSuccess()) {
			andView.setViewName("erp/customerList");
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
			andView.addObject("queryVO", queryVO);
			andView.addObject("pr", pr);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 进入客户信息，提供给公司工程、项目 编辑页面
	 * </pre>
	 * 
	 * @param customerId
	 * @return
	 */
	@GetMapping(value = "edit/{customerId}")
	public ModelAndView edit(@PathVariable Integer customerId) {
		ModelAndView andView = new ModelAndView();

		Resp<CustomerDO> resp = customerAO.queryById(customerId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/customerEdit");
			andView.addObject("customerDO", resp.getData());
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 编辑保存客户信息，提供给公司工程、项目信息
	 * </pre>
	 * 
	 * @param customerId
	 * @return
	 */
	@PostMapping(value = "edit")
	public ModelAndView edit(CustomerDO customerDO, BindingResult error, @RequestParam Map<String, String> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = customerAO.updateCustomer(customerDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/customer/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("erp/customerEdit");
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
			andView.addObject("customerDO", customerDO);
		}
		return andView;
	}

	/**
	 * 查询默认2000条记录的客户信息，提供下拉使用
	 * 
	 * @return
	 */
	@GetMapping(value = "queryList")
	@ResponseBody
	public Resp<List<CustomerDO>> queryList() {
		return customerAO.queryList();
	}

}
